package AssetCategory

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_api/utils/tree"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
	"property_api/internal/svc"
	"property_api/internal/types"
)

type (
	PagesLogic struct {
		logx.Logger
		ctx    context.Context
		svcCtx *svc.ServiceContext
	}
	dept struct {
		types.AssetCategory
		Children []tree.NodeWithChildren `json:"children"`
	}
)

func (d *dept) GetID() int64 {
	//TODO implement me
	return d.ID
}

func (d *dept) GetParentID() int64 {
	//TODO implement me
	return d.ParentID
}

func (d *dept) GetChildren() []tree.NodeWithChildren {
	//TODO implement me
	return d.Children
}

func (d *dept) SetChildren(children []tree.NodeWithChildren) {
	d.Children = children
}

func NewPagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PagesLogic {
	return &PagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Pages 查询资产分类的分页逻辑
func (l *PagesLogic) Pages(req *types.AssetCategoryPageReq) (resp *types.PageRes, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 调用服务层方法，查询资产分类记录的分页数据
	// 服务层方法 AssetCategoryPage 会处理实际的数据库查询操作
	data, err := l.svcCtx.BasicService.AssetCategoryPage(l.ctx, &property_service.AssetCategoryPageReq{
		Page:  req.Page,  // 当前页码
		Limit: req.Limit, // 每页条数
		Where: &property_service.AssetCategory{
			ID:                    req.ID,                    // 记录ID
			TenantID:              tenantID,                  // 租户ID
			ParentID:              req.ParentID,              // 父级ID
			ParentCode:            req.ParentCode,            //父级编码
			ParentLs:              req.ParentLs,              // 父级列表
			CategoryCode:          req.CategoryCode,          // 分类编号
			CustomCodeFlag:        req.CustomCodeFlag,        // 编号规则
			Name:                  req.Name,                  // 分类名称
			PinyinCode:            req.PinyinCode,            // 拼音简码
			DepreciationMonths:    req.DepreciationMonths,    // 折旧年限
			ManufacturerID:        req.ManufacturerID,        // 生产厂商ID
			ManufacturerName:      req.ManufacturerName,      // 生产厂商名称
			FinancialCategoryID:   req.FinancialCategoryID,   // 财务分类id
			FinancialCategoryName: req.FinancialCategoryName, // 财务分类名称
			BrandID:               req.BrandID,               // 品牌id
			BrandName:             req.BrandName,             // 品牌名称
			Status:                req.Status,                // 状态
			StatusName:            req.StatusName,            // 状态名称
			UnitPrice:             req.UnitPrice,             // 单价
			Unit:                  req.Unit,                  // 单位
			IsComponent:           req.IsComponent,           // 部件标志
			NeedMaintenance:       req.NeedMaintenance,       // 预防性维护
			Remark:                req.Remark,                // 备注
			StockID:               req.StockID,               // 仓库ID
			AssetTypeID:           req.AssetTypeID,           //资产类型id
			AssetTypeName:         req.AssetTypeName,         //资产类型名称
		},
	})
	//查询生产厂商id

	if req.Tree != 1 {
		return result.SucPage(data, types.AssetCategory{}).PageEd()
	}
	// 返回分页数据的响应
	return result.SucPage(data, dept{}, func(value interface{}) interface{} {
		data := value.([]*dept)
		return tree.MakeTreeByID(func() []tree.NodeWithChildren {
			convertedNodes := make([]tree.NodeWithChildren, 0)
			for _, category := range data {
				convertedNodes = append(convertedNodes, category)
			}
			return convertedNodes
		}())
	}).PageEd()
}
